home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / animutil / fastgfx / fg303b / manuals.arj / USER09.DOC < prev    next >
Encoding:
Text File  |  1993-10-02  |  44.1 KB  |  982 lines

  1. Chapter 9
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Image Files
  8.  
  9. 172   Fastgraph User's Guide
  10.  
  11.  
  12. Overview
  13.  
  14.      Within the context of Fastgraph, an image is a rectangular area
  15. containing some type of picture.  An image might be something as simple as a
  16. pointing hand icon, or as detailed as the dashboard of a sports car.
  17. Fastgraph includes several routines to display, retrieve, and manipulate
  18. images.  In this chapter we'll begin our discussion of images by looking at
  19. the image file formats Fastgraph supports, as well as the routines available
  20. for displaying and creating image files.
  21.  
  22.  
  23. PCX Files
  24.  
  25.      The PCX file format was originally developed by ZSoft Corporation for
  26. their commercial paint program, PC Paintbrush.  It has evolved into one of
  27. the more popular image file formats because so many products can read and
  28. write PCX files to at least some extent.  Fastgraph includes routines for
  29. displaying and creating PCX files, as well as other PCX support functions.
  30.  
  31.      The fg_showpcx routine displays an image stored in a PCX file.  It can
  32. position the image using the coordinate information in the PCX header, or
  33. such that its upper left corner is at the graphics cursor position on the
  34. active video page.  The first argument to fg_showpcx is the name of the PCX
  35. file (it may include a path name), and its second argument is a 16-bit mask
  36. that controls how the image is displayed.  The file name must be terminated
  37. with a null character, so BASIC, FORTRAN, and Pascal programmers will need to
  38. store a zero byte as the last character of the file name string.
  39.  
  40.      In the current version of Fastgraph, only the low-order two bits (bits 0
  41. and 1) of the bit mask argument are meaningful.  The following table
  42. summarizes the meanings of these bits.
  43.  
  44.               Bit Value  Meaning
  45.  
  46.                0    0    Use palette values stored in the PCX file
  47.                0    1    Use the current palette settings
  48.                1    0    Display image at position indicated in PCX header
  49.                1    1    Display image at current graphics position
  50.  
  51. All other bits are reserved and should be zero to guarantee compatibility
  52. with future releases.  The fg_showpcx routine returns a value of 0 if
  53. successful, 1 if the specified file wasn't found, and 2 if the file is not a
  54. PCX file.
  55.  
  56.      The fg_makepcx routine creates a PCX file from the specified rectangular
  57. region of the active video page.  Its first four arguments define the minimum
  58. x, maximum x, minimum y, and maximum y screen space coordinates of the region
  59. (the minimum x coordinate is reduced to a byte boundary if necessary).  Its
  60. fifth argument is the name of the PCX file to create (it may include a path
  61. name).  As with fg_showpcx, the file name must be terminated with a null
  62. character.  If an identically named file exists, it is overwritten.  The
  63. fg_makepcx routine returns a value of 0 if successful, and 1 if the PCX file
  64. was not created.
  65.  
  66.      Example 9-1 uses the fg_showpcx and fg_makepcx routines to create a new
  67. PCX file from selected rows of an existing 256-color 320 by 200 PCX file.  As
  68.                                                  Chapter 9:  Image Files   173
  69.  
  70.  
  71. written, the program uses the file CORAL.PCX to create NEW.PCX, but it could
  72. easily be extended to work with any PCX files.  The call to fg_showpcx
  73. displays the image in CORAL.PCX using the screen position and palette
  74. settings defined in the PCX file.  After waiting for a keystroke, the program
  75. calls fg_makepcx to create a PCX file named NEW.PCX from pixel rows 80
  76. through 99 of the original image.  In case the program encounters any
  77. problems, it prints an error message before exiting.
  78.  
  79.                                  Example 9-1.
  80.  
  81.            #include <fastgraf.h>
  82.            #include <stdio.h>
  83.            #include <stdlib.h>
  84.            void main(void);
  85.  
  86.            void main()
  87.            {
  88.               int old_mode;
  89.               int read_status, write_status;
  90.  
  91.               if (fg_testmode(19,1) == 0) {
  92.                  printf("This program requires a 320 ");
  93.                  printf("x 200 MCGA graphics mode.\n");
  94.                  exit(1);
  95.                  }
  96.               old_mode = fg_getmode();
  97.               fg_setmode(19);
  98.  
  99.               read_status = fg_showpcx("CORAL.PCX",0);
  100.               fg_waitkey();
  101.               if (read_status == 0)
  102.                  write_status = fg_makepcx(0,319,80,99,"NEW.PCX");
  103.               else
  104.                  write_status = 1;
  105.  
  106.               fg_setmode(old_mode);
  107.               fg_reset();
  108.  
  109.               if (read_status == 1)
  110.                  printf("CORAL.PCX not found.\n");
  111.               else if (read_status == 2)
  112.                  printf("CORAL.PCX is not a PCX file.\n");
  113.               if (write_status == 1)
  114.                  printf("NEW.PCX not created.\n");
  115.            }
  116.  
  117.  
  118.      In the Tandy/PCjr 16-color graphics mode (mode 9) and the native EGA
  119. graphics modes (modes 13 through 16), the palette registers are not readable.
  120. Hence, fg_makepcx will use the default palette settings when used in these
  121. video modes on Tandy and EGA systems.  Displaying a PCX file at a lower
  122. resolution (for example, a 640x480 PCX file at 320x200) will truncate the
  123. display on the right and on the bottom.  This effectively displays the upper
  124. left corner of the image.  The fg_showpcx and fg_makepcx routines have no
  125. effect in text video modes or in the Hercules low-resolution graphics mode.
  126. 174   Fastgraph User's Guide
  127.  
  128.      Because their structure parallels that of video memory, PCX files are
  129. specific to certain video modes.  If you try to display a PCX file in an
  130. incompatible video mode, fg_showpcx will still display something, but it will
  131. be garbled.  The following table summarizes the compatible video modes for
  132. PCX files.
  133.  
  134.           If PCX file was     You can display
  135.           created in mode     it in these modes
  136.  
  137.           4, 5                4, 5
  138.           6, 11               6, 11, 13-18, 28, 29
  139.           9                   9
  140.           13-18               13-18, 28, 29
  141.           19-27               19-27
  142.           28-29               13-18, 28, 29
  143.  
  144.      Fastgraph includes a function fg_pcxmode that determines the optimal
  145. video mode for displaying a PCX file.  By optimal, we mean the compatible
  146. video mode having the lowest resolution larger than or equal to the image
  147. dimensions.  The fg_pcxmode routine has a single argument -- the address of a
  148. buffer that contains a 128-byte PCX file header.  Specific values defined in
  149. certain fields of the PCX header determine which video mode is optimal.  A
  150. positive return value from fg_pcxmode represents the optimal video mode
  151. number.  The possible error returns are -1 if the buffer does not contain a
  152. valid PCX header, and -2 if fg_pcxmode cannot find any compatible video mode.
  153.  
  154.      How do we get the header from a PCX file into the buffer passed to
  155. fg_pcxmode?  The easiest way is with the fg_pcxhead function.  Its first
  156. argument is the name of the PCX file from which to retrieve the header (as
  157. before, the file name must be null-terminated).  The second argument is the
  158. address of the buffer that will receive the PCX header.  The size of this
  159. buffer must be at least 128 bytes.  If successful, fg_pcxmode returns zero.
  160. Otherwise, the return value is -1 if the specified file could not be opened,
  161. or -2 if the specified file is not a PCX file.  After successfully reading
  162. the PCX header, you can pass it to fg_pcxmode to determine the optimal video
  163. mode for the PCX file.  Example 9-2 demonstrates this process.
  164.  
  165.                                  Example 9-2.
  166.  
  167.                #include <fastgraf.h>
  168.                #include <stdio.h>
  169.                #include <stdlib.h>
  170.                void main(void);
  171.  
  172.                main()
  173.                {
  174.                   int mode, status;
  175.                   char header[128];
  176.  
  177.                   status = fg_pcxhead("CORAL.PCX",header);
  178.                   if (status == -1) {
  179.                      printf("Can't open CORAL.PCX.\n");
  180.                      exit(1);
  181.                   }
  182.                   else if (status == -2) {
  183.                      printf("CORAL.PCX is not a PCX file.\n");
  184.                      exit(1);
  185.                                                  Chapter 9:  Image Files   175
  186.  
  187.  
  188.                   }
  189.  
  190.                   mode = fg_pcxmode(header);
  191.                   printf("Optimal display mode is %d.\n",mode);
  192.               }
  193.  
  194.  
  195. GIF Files
  196.  
  197.      The GIF file format was originally created by CompuServe, Inc., as a
  198. transmission format for images and graphics data across the CompuServe
  199. network.  It has evolved into what is probably the most popular image file
  200. format in use today.  GIF files are especially prevalent on bulletin boards
  201. and electronic data networks because their efficient image compression
  202. results in less storage space and faster transmission times than other image
  203. file formats.  GIF, pronounced "jif", is an acronym for Graphics Interchange
  204. Format.  The format is the copyright property of CompuServe, Inc., whose GIF
  205. specification "grants a limited, non-exclusive, royalty-free license for the
  206. use of the Graphics Interchange Format in computer software; computer
  207. software utilizing GIF must acknowledge ownership of the Graphics Interchange
  208. Format and its Service Mark by CompuServe, Inc., in user and technical
  209. documentation".
  210.  
  211.      Two GIF specifications, released in 1987 and 1989, are defined.  The
  212. 1989 specification (known as "89a") is a superset of the original 1987
  213. specification (known as "87a").  Fastgraph's GIF file display routine can
  214. handle either 87a or 89a files.  For maximum portability, the GIF file
  215. creation routine always produces files conforming to the 87a specification.
  216.  
  217.      The fg_showgif routine displays an image stored in a GIF file.  It can
  218. position the image using the coordinate information in the GIF header, or
  219. such that its upper left corner is at the graphics cursor position on the
  220. active video page.  The fg_showgif arguments are the same as for fg_showpcx,
  221. except the file name must of course reference a GIF file rather than a PCX
  222. file.  Likewise, the fg_showgif return values are analogous to those of
  223. fg_showpcx.
  224.  
  225.      The fg_makegif routine creates a GIF file from the specified rectangular
  226. region of the active video page.  Its arguments and return value are
  227. analogous to those of fg_makepcx.
  228.  
  229.      Example 9-3 uses the fg_showgif and fg_makegif routines to create a new
  230. GIF file from selected rows of an existing 256-color 320 by 200 GIF file.
  231. Similar to example 9-1, the program uses the file CORAL.GIF to create
  232. NEW.GIF, but it could easily be extended to work with any GIF files.  The
  233. call to fg_showgif displays the image in CORAL.GIF using the screen position
  234. and palette settings defined in the GIF file.  After waiting for a keystroke,
  235. the program calls fg_makegif to create a GIF file named NEW.GIF from pixel
  236. rows 80 through 99 of the original image.  In case the program encounters any
  237. problems, it prints an error message before exiting.
  238.  
  239.                                  Example 9-3.
  240.  
  241.            #include <fastgraf.h>
  242.            #include <stdio.h>
  243.            #include <stdlib.h>
  244. 176   Fastgraph User's Guide
  245.  
  246.  
  247.            void main(void);
  248.  
  249.            void main()
  250.            {
  251.               int old_mode;
  252.               int read_status, write_status;
  253.  
  254.               if (fg_testmode(19,1) == 0) {
  255.                  printf("This program requires a 320 ");
  256.                  printf("x 200 MCGA graphics mode.\n");
  257.                  exit(1);
  258.                  }
  259.               old_mode = fg_getmode();
  260.               fg_setmode(19);
  261.  
  262.               read_status = fg_showgif("CORAL.GIF",0);
  263.               fg_waitkey();
  264.               if (read_status == 0)
  265.                  write_status = fg_makegif(0,319,80,99,"NEW.GIF");
  266.               else
  267.                  write_status = 1;
  268.  
  269.               fg_setmode(old_mode);
  270.               fg_reset();
  271.  
  272.               if (read_status == 1)
  273.                  printf("CORAL.GIF not found.\n");
  274.               else if (read_status == 2)
  275.                  printf("CORAL.GIF is not a GIF file.\n");
  276.               if (write_status == 1)
  277.                  printf("NEW.GIF not created.\n");
  278.            }
  279.  
  280.  
  281.      Like fg_makepcx, the fg_makegif routine will use the default palette
  282. settings when used on Tandy and EGA systems.  Displaying a GIF file at a
  283. lower resolution (for example, a 640x480 GIF file at 320x200) will truncate
  284. the display on the right and on the bottom.  This effectively displays the
  285. upper left corner of the image.  Unlike PCX files, GIF files do not exhibit
  286. compatibility problems between 16-color and 256-color graphics modes.  When
  287. fg_showgif displays a 256-color GIF in a 16-color mode, it displays pixels of
  288. color c in color c mod 16.  The fg_showgif and fg_makegif routines have no
  289. effect in text video modes, or in CGA and Hercules graphics modes.
  290.  
  291.  
  292. Pixel Run Files
  293.  
  294.      Fastgraph also provides its own mode-independent image file format
  295. called pixel run format.  Pixel run files are useful in programs that must
  296. run in different video modes (but with the same resolution) because you can
  297. use the same image files for two-color modes as for 256-color modes.  Two
  298. variations of the pixel run format exist -- standard pixel run files (SPR
  299. files) and packed pixel run files (PPR files).  The packed pixel run format
  300. does not support 256-color images but will produce a smaller file if the
  301. image has 16 colors or less.  Pixel run files do not include a header or any
  302. color palette information.
  303.                                                  Chapter 9:  Image Files   177
  304.  
  305.  
  306.      The best way to illustrate the pixel run file format is with an example.
  307. Suppose we want to display a small triangle whose perimeter is a different
  308. color than its interior.  To create the standard pixel run equivalent of this
  309. image, we must inscribe the triangle in a rectangular area.  Hence, the pixel
  310. representation of our triangle might appear as shown below.
  311.  
  312.                               . . . . * . . . .
  313.                               . . . * x * . . .
  314.                               . . * x x x * . .
  315.                               . * x x x x x * .
  316.                               * * * * * * * * *
  317.  
  318.      As shown in this diagram, our triangle image is nine pixels wide at its
  319. base and five pixels high.  The pixels indicated by an asterisk (*) are the
  320. triangle's perimeter, while those indicated by an x represent its interior
  321. points.  The pixels shown as periods (.) are not part of the triangle itself,
  322. but they are part of the image.  In this example, we can treat them as
  323. background pixels.
  324.  
  325.      If we start at the lower left corner of the image and proceed to the
  326. right, we could represent the first row of the image as nine pixels of color
  327. "asterisk".  Such a group of consecutive identically colored pixels is called
  328. a pixel run, so a single pixel run describes the first row of the image.  The
  329. row above this one is a bit more complex.  It consists of five pixel runs:
  330. one pixel of color "period", followed by one of color "asterisk", then five
  331. of color "x", one of color "asterisk", and finally one of color "period".
  332.  
  333.      While we could construct separate pixel runs for each row of the image,
  334. notice that three of the five rows in our triangle begin with the same color
  335. pixel as the rightmost pixel in the previous row.  Fastgraph's pixel run
  336. formats let you take advantage of this property by allowing pixel runs to
  337. wrap from one row to the next.  This means we can represent the pixel run of
  338. color "period" extending from the right side of the second row to the left
  339. side of the third row as a single run of three pixels.
  340.  
  341.      An standard pixel run (SPR) file is nothing more than such a sequence of
  342. (color,count) pairs, as shown in the following diagram.
  343.  
  344.  
  345.                           byte 0   color for run 1
  346.  
  347.                                1   count for run 1
  348.  
  349.                                2   color for run 2
  350.  
  351.                                3   count for run 2
  352.                                           .
  353.                                           .
  354.                                           .
  355.                                           .
  356.                                           .
  357.                             2n-2   color for run n
  358.  
  359.                             2n-1   count for run n
  360.  
  361. 178   Fastgraph User's Guide
  362.  
  363. Each color is a value between 0 and 255 specifying the color index for that
  364. pixel run.  Each count is a value between 0 and 255 specifying the length in
  365. pixels of that pixel run.  If a single run exceeds 255 pixels, it must be
  366. broken into two or more runs.  For example, we could represent a pixel run of
  367. length 265 as a run of length 255 followed by a run of length 10 of the same
  368. color.  Note that the space in bytes needed to store an SPR image is twice
  369. the number of runs.
  370.  
  371.      Fastgraph's fg_showspr routine displays an SPR file.  Its first argument
  372. is the name of the file containing the image (it may include a path name).
  373. The file name must be terminated with a null character, so BASIC, FORTRAN,
  374. and Pascal programmers will need to store a zero byte as the last character
  375. of the file name string.  The second argument is the width in pixels of the
  376. image.  The fg_showspr routine displays the image such that its lower left
  377. corner is at the graphics cursor position.  The possible return values for
  378. fg_showspr are success (0) and file not found (1).  To create an SPR file,
  379. use the fg_makespr routine.  Its arguments and return values are the same as
  380. for fg_makepcx and fg_makegif.
  381.  
  382.      Example 9-4 uses the fg_showspr and fg_makespr routines to create a new
  383. SPR file from selected rows of an existing 16-color 320 by 200 SPR file.
  384. Similar to examples 9-1 and 9-3, the program uses the file CORAL.SPR to
  385. create NEW.SPR, but it could easily be extended to work with any SPR files.
  386. The call to fg_move to establishes the lower left corner of the screen as the
  387. graphics cursor position (contrast this with the upper left corner being the
  388. reference point for PCX and GIF files).  Then program then calls fg_showspr
  389. to display the image.  After waiting for a keystroke, the program calls
  390. fg_makespr to create an SPR file named NEW.SPR from pixel rows 80 through 99
  391. of the original image.  In case the program encounters any problems, it
  392. prints an error message before exiting.
  393.  
  394.                                  Example 9-4.
  395.  
  396.            #include <fastgraf.h>
  397.            #include <stdio.h>
  398.            #include <stdlib.h>
  399.            void main(void);
  400.  
  401.            void main()
  402.            {
  403.               int new_mode, old_mode;
  404.               int read_status, write_status;
  405.  
  406.               new_mode = fg_bestmode(320,200,1);
  407.               if (new_mode < 0 || new_mode == 12) {
  408.                  printf("This program requires a 320 ");
  409.                  printf("x 200 color graphics mode.\n");
  410.                  exit(1);
  411.                  }
  412.               old_mode = fg_getmode();
  413.               fg_setmode(new_mode);
  414.               fg_move(0,199);
  415.  
  416.               read_status = fg_showspr("CORAL.SPR",320);
  417.               fg_waitkey();
  418.               if (read_status == 0)
  419.                  write_status = fg_makespr(0,319,80,99,"NEW.SPR");
  420.                                                  Chapter 9:  Image Files   179
  421.  
  422.  
  423.               else
  424.                  write_status = 1;
  425.  
  426.               fg_setmode(old_mode);
  427.               fg_reset();
  428.  
  429.               if (read_status == 1)
  430.                  printf("CORAL.SPR not found.\n");
  431.               if (write_status == 1)
  432.                  printf("NEW.SPR not created.\n");
  433.            }
  434.  
  435.  
  436.      If you have an image that only uses the first 16 color indices (0 to
  437. 15), you can use Fastgraph's packed pixel run (PPR) image format.  This
  438. format packs two color values into each color byte, so it takes three bytes
  439. instead of four to represent two pixel runs.  This means a PPR file is 25%
  440. smaller than its SPR equivalent.  In each set of three bytes, the high four
  441. bits of the first byte contain the color of the first run, and the low four
  442. bits contain the color of the second run.  The second byte contains the
  443. length of the first run, and the third byte contains the length of the second
  444. run.
  445.  
  446.      The following diagram illustrates the structure of the packed pixel
  447. file.  In this example, the file is assumed to contain n pixel runs, where n
  448. is an even number.  If n is odd, the byte offset for the last element is 3n/2
  449. (truncated) instead of 3n/2-1, and the low four bits of the last color byte
  450. (that is, the color for pixel run n+1) are ignored.
  451.  
  452.                       7                4   3                0
  453.  
  454.                byte 0    color for run 1     color for run 2
  455.  
  456.                     1              count for run 1
  457.  
  458.                     2              count for run 2
  459.  
  460.                     3    color for run 3     color for run 4
  461.  
  462.                     4              count for run 3
  463.  
  464.                     5              count for run 4
  465.                                        .
  466.                                        .
  467.                                        .
  468.                                        .
  469.                                        .
  470.                3n/2-3   color for run n-1    color for run n
  471.  
  472.                3n/2-2             count for run n-1
  473.  
  474.                3n/2-1              count for run n
  475.  
  476.  
  477.      The structure of the PPR file allows for color values between 0 and 15,
  478. and run lengths between 0 and 255.  The space in bytes needed to store an
  479. 180   Fastgraph User's Guide
  480.  
  481.  
  482. image in PPR format is 1.5 times the number of runs, compared to twice the
  483. number of runs for the SPR format.
  484.  
  485.      The fg_showppr and fg_makeppr routines display and create PPR files,
  486. respectively.  Their arguments and return values are the same as those of
  487. fg_showspr and fg_makespr.  If we wanted to display PPR files instead of SPR
  488. files in example 9-4, all that's necessary is changing the fg_showspr and
  489. fg_makespr calls to fg_showppr and fg_makeppr.
  490.  
  491.      Fastgraph's fg_dispfile routine displays both SPR and PPR files.  The
  492. first of its three arguments is the name of the image file (it may include a
  493. path name).  The file name must be terminated with a null character, so
  494. BASIC, FORTRAN, and Pascal programmers will need to store a zero byte as the
  495. last character of the file name string.  The second argument is the image
  496. width in pixels, and the third argument defines the image format (that is,
  497. SPR or PPR).  As with other pixel run display routines, fg_dispfile displays
  498. the image such that its lower left corner is at the graphics cursor position.
  499.  
  500.      Example 9-5 illustrates how to use the fg_dispfile routine to display an
  501. image stored in a pixel run file.  The program displays two identical images,
  502. one in an SPR file and the other in a PPR file.  Each image is a picture of
  503. the sea floor and some coral, as might be used for the background in an
  504. aquarium.  The program runs in a 320 by 200 graphics mode, and the images
  505. fill the entire screen.
  506.  
  507.      The SPR image is in file CORAL.SPR.  The program uses the fg_move
  508. routine to establish the lower left corner of the screen as the graphics
  509. cursor position and then calls fg_dispfile to display the image.  The value
  510. of fg_dispfile's third argument tells Fastgraph the image format.  A value of
  511. 0 indicates the file contains an image in SPR format, while a value of 1
  512. indicates an image in PPR format.  As mentioned earlier, the image fills the
  513. entire screen, so its width is 320 pixels.
  514.  
  515.      After waiting for a keystroke, the program clears the previous image
  516. from the screen and then calls fg_dispfile to display the PPR image from the
  517. file CORAL.PPR.  The program leaves the second image on the screen until
  518. another keypress, at which time it restores the original video mode and
  519. screen attributes and returns to DOS.
  520.  
  521.                                  Example 9-5.
  522.  
  523.                 #include <fastgraf.h>
  524.                 #include <stdio.h>
  525.                 #include <stdlib.h>
  526.                 void main(void);
  527.  
  528.                 void main()
  529.                 {
  530.                    int old_mode, new_mode;
  531.  
  532.                    new_mode = fg_bestmode(320,200,1);
  533.                    if (new_mode < 0 || new_mode == 12) {
  534.                       printf("This program requires a 320 ");
  535.                       printf("x 200 color graphics mode.\n");
  536.                       exit(1);
  537.                       }
  538.                                                  Chapter 9:  Image Files   181
  539.  
  540.  
  541.                    old_mode = fg_getmode();
  542.                    fg_setmode(new_mode);
  543.  
  544.                    fg_move(0,199);
  545.                    fg_dispfile("CORAL.SPR",320,0);
  546.                    fg_waitkey();
  547.  
  548.                    fg_erase();
  549.                    fg_dispfile("CORAL.PPR",320,1);
  550.                    fg_waitkey();
  551.  
  552.                    fg_setmode(old_mode);
  553.                    fg_reset();
  554.                 }
  555.  
  556.  
  557.      The SNAPSHOT utility distributed with Fastgraph is a terminate and stay
  558. resident program (TSR) that can capture graphics mode screen images and save
  559. them in SPR files.  Thus, you can easily create files with SNAPSHOT and
  560. display them with the fg_showspr or fg_dispfile routines.  Another TSR
  561. utility, GrabRGB, is useful for capturing RGB color values from 256-color
  562. images.  Appendix A contains complete descriptions of the SNAPSHOT and
  563. GrabRGB utilities.
  564.  
  565.  
  566. Display Patterns
  567.  
  568.      Example 9-5 works well in the graphics video modes with 16 or 256
  569. available colors.  However, in the four-color CGA graphics modes the
  570. resulting image is not too good because of our limited color choices, and it
  571. would look even worse in the Hercules graphics mode.  The Fastgraph routine
  572. fg_pattern allows you to associate a dither pattern (actually, any pixel
  573. sequence) with one of Fastgraph's 256 color indices appearing in a pixel run
  574. map.  When displaying an SPR or PPR file, Fastgraph will use the pattern
  575. associated with that color index instead of displaying the color itself.
  576.  
  577.      The fg_pattern routine requires two integer arguments -- a color index
  578. (between 0 and 255) and the display pattern defined for that color index.  A
  579. display pattern's structure resembles the structure of video memory and is
  580. thus dependent on the current video mode.  The following sections list the
  581. initial display patterns and explain how to construct new display patterns
  582. for different graphics video modes.
  583.  
  584. CGA four-color graphics modes
  585.  
  586.      In the four-color CGA graphics modes (modes 4 and 5), the display
  587. pattern is a 16-bit quantity consisting of an 8-bit shift count followed by
  588. an 8-bit pixel pattern.  Each pixel assumes a value between 0 and 3, so the
  589. pattern represents four pixels.  In even-numbered pixel rows, Fastgraph uses
  590. the pixel pattern itself.  In odd-numbered pixel rows, Fastgraph rotates the
  591. original pattern to the left by the number of bits specified by the shift
  592. count.
  593.  
  594.      For example, if we are using the default CGA color palette, we could
  595. create a darker shade of cyan by alternating cyan pixels (color 1, 01 binary)
  596. with white pixels (color 3, 11 binary), as shown below.
  597. 182   Fastgraph User's Guide
  598.  
  599.  
  600.  
  601.                                  01 11 01 11
  602.  
  603.  
  604. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  605. value 77.
  606.  
  607.      To complete the display pattern, we need to determine the shift count.
  608. If we use a shift count of zero, the resulting display will simply be a
  609. series of cyan and white vertical lines.  What we really need is a
  610. checkerboard effect where a white pixel is above and below each cyan pixel,
  611. and vice versa.  If we rotate the pattern one pixel (two bits) to the left,
  612. we will achieve the desired effect.  That is, a shift count of two produces
  613. the following pixel patterns:
  614.  
  615.                        even-numbered rows   01 11 01 11
  616.                         odd-numbered rows   11 01 11 01
  617.  
  618. Combining the shift count with the pixel pattern yields the display pattern
  619. 0277 hex.  The shift count is normally a multiple of two; note that a zero
  620. shift count results in the same pattern being applied to all pixel rows.
  621.  
  622.      For the CGA four-color graphics modes, the fg_setmode routine
  623. establishes the following initial display patterns:
  624.  
  625.                   color  shift count    hexadecimal
  626.                   index  and pattern    equivalent
  627.  
  628.                     0    0 00000000        0000
  629.                     1    0 01010101        0055
  630.                     2    0 10101010        00AA
  631.                     3    0 11111111        00FF
  632.  
  633. These values are repeated as necessary to define color indices 4 to 255.
  634. That is, colors 4, 8, 12, ... , 252 use the same defaults as color 0.  Colors
  635. 5, 9, 13, ... , 253 use the same defaults as color 1, and so forth.  Also
  636. note that pattern 0000 represents four pixels of color 0, 0055 represents
  637. four pixels of color 1, 00AA represents four pixels of color 2, and 00FF
  638. represents four pixels of color 3.
  639.  
  640. CGA two-color graphics mode
  641.  
  642.      In the two-color CGA graphics mode (mode 6), the display pattern is also
  643. a 16-bit quantity consisting of an 8-bit shift count followed by an 8-bit
  644. pixel pattern.  Each pixel assumes the value 0 or 1, so the pattern
  645. represents eight pixels.  In even-numbered pixel rows, Fastgraph uses the
  646. pixel pattern itself.  In odd-numbered pixel rows, Fastgraph rotates the
  647. original pattern to the left by the number of bits specified by the shift
  648. count.
  649.  
  650.      For example, we could create a lighter shade of white by alternating
  651. black pixels (color 0) with white pixels (color 1), as shown below.
  652.  
  653.  
  654.                                0 1 0 1 0 1 0 1
  655.  
  656.                                                  Chapter 9:  Image Files   183
  657.  
  658.  
  659. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  660. value 55.
  661.  
  662.      To complete the display pattern, we need to determine the shift count.
  663. We must rotate the pattern one pixel (one bit) to the left to achieve the
  664. checkerboard effect as in the CGA four color graphics modes.  That is, a
  665. shift count of one produces the following pixel patterns:
  666.  
  667.                      even-numbered rows   0 1 0 1 0 1 0 1
  668.                       odd-numbered rows   1 0 1 0 1 0 1 0
  669.  
  670. Combining the shift count with the pixel pattern yields the display pattern
  671. 0155 hex.
  672.  
  673.      For the CGA two-color graphics mode, the fg_setmode routine establishes
  674. the initial display patterns such that all even-numbered color indices are
  675. assigned the value 0000, while all odd-numbered color indices are assigned
  676. the value 00FF.  Note that pattern 0000 represents eight pixels of color 0,
  677. and 00FF represents eight pixels of color 1.
  678.  
  679. Tandy/PCjr 16-color graphics mode
  680.  
  681.      In the Tandy/PCjr 16-color graphics mode (mode 9), the display pattern
  682. is also 16-bit quantity consisting of an 8-bit shift count followed by an 8-
  683. bit pixel pattern.  Each pixel assumes a value between 0 and 15, so the
  684. pattern represents two pixels.  In even-numbered pixel rows, Fastgraph uses
  685. the pixel pattern itself.  In odd-numbered pixel rows, Fastgraph rotates the
  686. original pattern to the left by the number of bits specified by the shift
  687. count.
  688.  
  689.      For example, we could create a lighter shade of blue by alternating blue
  690. pixels (color 1, 0001 binary) with white pixels (color 15, 1111 binary), as
  691. shown below.
  692.  
  693.                                   0001 1111
  694.  
  695. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  696. value 1F.
  697.  
  698.      To complete the display pattern, we need to determine the shift count.
  699. Using the same process as in the CGA graphics modes, we must rotate the
  700. pattern one pixel (four bits) to the left to achieve the checkerboard effect.
  701. That is, a shift count of four produces the following pixel patterns:
  702.  
  703.                         even-numbered rows   0001 1111
  704.                          odd-numbered rows   1111 0001
  705.  
  706. Combining the shift count with the pixel pattern yields the display pattern
  707. 041F hex.  The shift count is normally zero or four; note that a zero shift
  708. count results in the same pattern being applied to all pixel rows.
  709.  
  710.      For the Tandy/PCjr 16-color graphics modes, the fg_setmode routine
  711. establishes the initial display patterns such that color 0 is assigned the
  712. value 0000 (two pixels of color 0), color 1 is assigned the value 0011 (two
  713. pixels of color 1), color 2 is assigned the value 0022 (two pixels of color
  714. 184   Fastgraph User's Guide
  715.  
  716.  
  717. 2), and so forth.  These values are repeated as necessary to define color
  718. indices 16 to 255.  That is, colors 0, 16, 32, ... , 240 use the same
  719. defaults as color 0.  Colors 1, 17, 33, ... , 241 use the same defaults as
  720. color 1, and so forth.
  721.  
  722. Hercules graphics modes
  723.  
  724.      The structure of the display patterns for the Hercules graphics modes
  725. (modes 11 and 12) is the same as two of the CGA graphics modes.  For the
  726. standard Hercules graphics mode (mode 11), please refer to the discussion of
  727. CGA two-color (mode 6) display patterns.  For the low-resolution Hercules
  728. graphics mode (mode 12), please refer to the discussion of the CGA four-color
  729. (mode 4) display patterns.
  730.  
  731. EGA/VGA/SVGA 16-color graphics modes
  732.  
  733.      In the EGA/VGA/SVGA 16-color graphics modes (modes 13 to 16, 18, 28, and
  734. 29), the display pattern is an 8-bit quantity consisting of two 4-bit color
  735. values (for consistency with the other video modes, we still pass the display
  736. pattern as a 16-bit quantity).  Each pixel assumes a value between 0 and 15
  737. (0 and 5 in the EGA monochrome graphics mode), so the pattern represents two
  738. pixels.  In even-numbered pixel rows, Fastgraph uses the pixel pattern
  739. itself.  In odd-numbered pixel rows, Fastgraph rotates the original pattern
  740. one pixel (four bits) to the left.
  741.  
  742.      For example, we could create a lighter shade of blue by alternating blue
  743. pixels (color 1, 0001 binary) with white pixels (color 15, 1111 binary), as
  744. shown below.
  745.  
  746.                                   0001 1111
  747.  
  748. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  749. value 1F.  The implied four-bit shift count produces the following pixel
  750. patterns:
  751.  
  752.                         even-numbered rows   0001 1111
  753.                          odd-numbered rows   1111 0001
  754.  
  755. Extending the pixel pattern to a 16-bit quantity yields the display pattern
  756. 001F hex.
  757.  
  758.      For the EGA/VGA/SVGA 16-color graphics modes, the fg_setmode routine
  759. establishes the initial display patterns such that color 0 is assigned the
  760. value 0000 (two pixels of color 0), color 1 is assigned the value 0011 (two
  761. pixels of color 1), color 2 is assigned the value 0022 (two pixels of color
  762. 2), and so forth.  These values are repeated as necessary to define color
  763. indices 16 to 255.  That is, colors 0, 16, 32, ... , 240 use the same
  764. defaults as color 0.  Colors 1, 17, 33, ... , 241 use the same defaults as
  765. color 1, and so forth.
  766.  
  767. MCGA/VGA 2-color graphics mode
  768.  
  769.      In the two-color MCGA/VGA graphics mode (mode 17), the display pattern
  770. is a 2-bit quantity consisting of two 1-bit color values (for consistency
  771. with the other video modes, we still pass the display pattern as a 16-bit
  772.                                                  Chapter 9:  Image Files   185
  773.  
  774.  
  775. quantity).  Each pixel assumes the value 0 or 1, so the pattern represents
  776. two pixels.  In even-numbered pixel rows, Fastgraph uses the pixel pattern
  777. itself.  In odd-numbered pixel rows, Fastgraph rotates the original pattern
  778. one pixel (one bit) to the left.
  779.  
  780.      For example, we could create a lighter shade of white by alternating
  781. black pixels (color 0) with white pixels (color 1), as shown below.
  782.  
  783.                                      0 1
  784.  
  785. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  786. value 01.  The implied one-bit shift count produces the following pixel
  787. patterns:
  788.  
  789.                            even-numbered rows   0 1
  790.                             odd-numbered rows   1 0
  791.  
  792. Extending the pixel pattern to a 16-bit quantity yields the display pattern
  793. 0001 hex.
  794.  
  795.      For the MCGA/VGA two-color graphics mode, the fg_setmode routine
  796. establishes the initial display patterns such that all even-numbered color
  797. indices are assigned the value 0000 (two pixels of color 0), while all odd-
  798. numbered color indices are assigned the value 0003 (11 binary, or two pixels
  799. of color 1).
  800.  
  801. 256-color graphics modes
  802.  
  803.      The 256-color graphics modes (modes 19 through 27) offer 262,144
  804. different colors, so dithering is seldom (if ever) required.  For this
  805. reason, the fg_pattern routine has no effect in these video modes.
  806.  
  807. An example
  808.  
  809.      Example 9-6 illustrates the use of display patterns in several graphics
  810. modes.  This program runs in any 320 by 200 color graphics mode and displays
  811. the CORAL.PPR image with one or more of the color indices redefined.  If the
  812. program runs in the standard CGA four-color mode (mode 4), it redefines the
  813. first 16 display patterns using the fg_pattern routine and the values in the
  814. CGApatterns array.  In the Tandy/PCjr 16-color graphics mode (mode 9) and the
  815. EGA low-resolution graphics mode (mode 13), the program redefines color index
  816. 15 to produce an alternating gray and white dither pattern.  In the MCGA 256-
  817. color mode (mode 19), display patterns are not available, so the program uses
  818. fg_setrgb to define color index 15 as slightly darker shade of gray than the
  819. default for color 7.
  820.  
  821.                                  Example 9-6.
  822.  
  823.                 #include <fastgraf.h>
  824.                 #include <stdio.h>
  825.                 #include <stdlib.h>
  826.                 void main(void);
  827.  
  828.                 int CGApatterns[] = {
  829.                    0x0000,0x00FF,0x00FF,0x00FF,
  830.  
  831. 186   Fastgraph User's Guide
  832.  
  833.  
  834.                    0x02BB,0x0000,0x0222,0x0255,
  835.                    0x00FF,0x00FF,0x00FF,0x0055,
  836.                    0x00AA,0x00AA,0x00FF,0x0277
  837.                    };
  838.  
  839.                 void main()
  840.                 {
  841.                    int color;
  842.                    int old_mode, new_mode;
  843.  
  844.                    new_mode = fg_bestmode(320,200,1);
  845.                    if (new_mode < 0 || new_mode == 12) {
  846.                       printf("This program requires a 320 ");
  847.                       printf("x 200 color graphics mode.\n");
  848.                       exit(1);
  849.                       }
  850.  
  851.                    old_mode = fg_getmode();
  852.                    fg_setmode(new_mode);
  853.  
  854.                    if (new_mode == 4) {
  855.                       fg_palette(0,0);
  856.                       for (color = 0; color < 16; color++)
  857.                          fg_pattern(color,CGApatterns[color]);
  858.                       }
  859.                    else if (new_mode == 9 || new_mode == 13)
  860.                       fg_pattern(15,0x04F7);
  861.                    else
  862.                       fg_setrgb(15,38,38,38);
  863.  
  864.                    fg_move(0,199);
  865.                    fg_showppr("CORAL.PPR",320);
  866.                    fg_waitkey();
  867.  
  868.                    fg_setmode(old_mode);
  869.                    fg_reset();
  870.                 }
  871.  
  872.  
  873. Controlling the Image Buffer Size
  874.  
  875.      By default, all of Fastgraph's image file display and creation routines
  876. use an internal 4,096-byte buffer.  This buffer provides an intermediate
  877. storage area, making it possible to perform more efficient buffered I/O when
  878. reading or writing the image files.  The fg_imagebuf routine lets you define
  879. your own buffer (up to 64K bytes) for this purpose.  Larger buffers generally
  880. make image display and creation faster.
  881.  
  882.      The fg_imagebuf routine does not allocate storage for the internal
  883. buffer.  Rather, it just defines the array or dynamically allocated memory
  884. block to be used as the buffer.  The first argument passed to fg_imagebuf is
  885. the far address of this array or memory block.  Using a far address doesn't
  886. waste valuable space in the default data segment, and almost always results
  887. in the ability to use a larger buffer.  In Pascal programs, the space for the
  888. internal buffer must be allocated dynamically with the GetMem procedure
  889. because it provides the only way to pass something by far reference in
  890.                                                  Chapter 9:  Image Files   187
  891.  
  892.  
  893. Pascal.  The second fg_imagebuf argument is the size of the internal buffer,
  894. represented as an unsigned integer.  QuickBASIC programs must pass the image
  895. buffer as a fixed-length string because that language does not support far
  896. pointers.
  897.  
  898.      Example 9-7 shows how to use fg_imagebuf to define a larger buffer when
  899. displaying the CORAL.PCX file.  In this example, we'll use a 20,000-byte
  900. static array as the image buffer.  This size was chosen because it's larger
  901. than the PCX file size, so fg_showpcx can read the entire PCX file in one
  902. pass.
  903.  
  904.                                  Example 9-7.
  905.  
  906.                 #include <fastgraf.h>
  907.                 #include <stdio.h>
  908.                 #include <stdlib.h>
  909.                 void main(void);
  910.  
  911.                 char far buffer[20000];
  912.  
  913.                 void main()
  914.                 {
  915.                    int old_mode;
  916.  
  917.                    if (fg_testmode(19,1) == 0) {
  918.                       printf("This program requires a 320 ");
  919.                       printf("x 200 MCGA graphics mode.\n");
  920.                       exit(1);
  921.                       }
  922.                    old_mode = fg_getmode();
  923.                    fg_setmode(19);
  924.                    fg_imagebuf(buffer,20000);
  925.                    fg_showpcx("CORAL.PCX",0);
  926.                    fg_waitkey();
  927.  
  928.                    fg_setmode(old_mode);
  929.                    fg_reset();
  930.                 }
  931.  
  932.  
  933.      You don't need to create separate buffers for each image you display or
  934. create.  Once you define an internal image buffer with fg_imagebuf, Fastgraph
  935. will use that buffer until your program exits, or until you call fg_imagebuf
  936. with a buffer size equal to zero.
  937.  
  938.  
  939. Summary of Image File Routines
  940.  
  941.      This section summarizes the functional descriptions of the Fastgraph
  942. routines presented in this chapter.  More detailed information about these
  943. routines, including their arguments and return values, may be found in the
  944. Fastgraph Reference Manual.  The image display and creation functions are
  945. applicable only to graphics video modes.
  946.  
  947.      FG_IMAGEBUF specifies the size and address of the buffer used internally
  948. when creating or displaying GIF, PCX, PPR, or SPR files.  Fastgraph's default
  949. 188   Fastgraph User's Guide
  950.  
  951.  
  952. internal buffer size is 4,096 bytes.  Image display or creation is typically
  953. faster when a larger buffer is used.
  954.  
  955.      FG_DISPFILE displays an image stored in a standard or packed pixel run
  956. file.  The image is positioned so that its lower left corner is at the
  957. current graphics position.
  958.  
  959.      FG_MAKEGIF creates a GIF file from the specified rectangular region of
  960. the active video page.  The region's extremes are expressed in screen space
  961. units.  This routine is meaningful only in 16-color and 256-color graphics
  962. modes.
  963.  
  964.      FG_MAKEPCX creates a PCX file from the specified rectangular region of
  965. the active video page.  The region's extremes are expressed in screen space
  966. units.
  967.  
  968.      FG_MAKEPPR creates a packed pixel run file from the specified
  969. rectangular region of the active video page.  The region's extremes are
  970. expressed in screen space units.
  971.  
  972.      FG_MAKESPR creates a standard pixel run file from the specified
  973. rectangular region of the active video page.  The region's extremes are
  974. expressed in screen space units.
  975.  
  976.      FG_PATTERN defines a display pattern for use when displaying pixel run
  977. files in video modes that offer 16 or less colors.
  978.  
  979.      FG_PCXHEAD reads a PCX file header into a 128-byte buffer.
  980.  
  981.      FG_PCXMODE determines the optimal video mode for displaying a PCX file.
  982. The optimal mode is the compatible video mode having the lowest resolution
  983. larger than or equal to the image dimensions.
  984.  
  985.      FG_SHOWGIF displays an image stored in a GIF file.  By default, the
  986. image will be positioned so its upper left corner is at the graphics cursor
  987. position of the active video page.  This routine is meaningful only in 16-
  988. color and 256-color graphics modes.
  989.  
  990.      FG_SHOWPCX displays an image stored in a PCX file.  By default, the
  991. image will be positioned so its upper left corner is at the graphics cursor
  992. position of the active video page.
  993.  
  994.      FG_SHOWPPR displays an image stored in a packed pixel run file.  The
  995. image will be positioned so its lower left corner is at the graphics cursor
  996. position of the active video page.
  997.  
  998.      FG_SHOWSPR displays an image stored in a standard pixel run file.  The
  999. image will be positioned so its lower left corner is at the graphics cursor
  1000. position of the active video page.
  1001.